// CountryController.java (控制器层)
package com.ywb.controller;

import com.ywb.anno.Log;
import com.ywb.pojo.Country;
import com.ywb.pojo.CountryQueryParam;
import com.ywb.pojo.PageResult;
import com.ywb.pojo.Result;
import com.ywb.service.CountryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/countries")
@RestController
public class CountryController {

    @Autowired
    private CountryService countryService;



    @GetMapping("/all")
    public Result list() {
        log.info("查询全部国家数据");
        List<Country> countries = countryService.findAll();
        return Result.success(countries);
    }

    @GetMapping
    public Result page(CountryQueryParam countryQueryParam) {
        log.info("分页查询，参数:", countryQueryParam);
        PageResult<Country> pageResult = countryService.page(countryQueryParam);
        return Result.success(pageResult);
    }
    @Log
    @DeleteMapping
    public Result delete(Integer id) {
        log.info("删除国家{}", id);
        countryService.deleteById(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Country country) {
        log.info("新增国家{}", country);
        countryService.add(country);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getinfo(@PathVariable("id") Integer countryId) {
        log.info("根据ID查询国家{}", countryId);
        Country country = countryService.getById(countryId);
        return Result.success(country);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Country country) {
        log.info("修改国家{}", country);
        countryService.update(country);
        return Result.success();
    }
}